<?php

require_once "application.php";

class KontaktController extends application {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_Kontakt();
        $form->setAction('/kontakt/')
                ->setMethod('post');
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $form->removeElement('captcha');
        }
        if ($_POST) {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
            } else {
                $dane = array(
                    'sender' => $_POST['sender'],
                    'email' => $_POST['email'],
                    'phone' => $_POST['phone'],
                    'subject' => $_POST['subject'],
                    'text' => $_POST['text'],
                );
                Zend_Loader::loadClass('Zend_Mail');
                $config = array(
                    'ssl' => 'tls',
                    'port' => '25',
                    'auth' => 'plain',
                    'username' => 'katalogi@tczewskie.pl',
                    'password' => 'ainet_cr_1987^');

                $mail = new Zend_Mail('utf-8');
                $transport = new Zend_Mail_Transport_Smtp('ainet.com.pl', $config);

                Zend_Mail::setDefaultTransport($transport);

                $mail->setBodyHtml($_POST['text']);

                $mail->setFrom($_POST['email'], $_POST['sender'] . ' ' . $_POST['phone'])
                        ->addTo('katalogi@tczewskie.pl', 'Katalogi Firm Tczew')
                        ->setSubject($_POST['subject'])
                        ->send();
                $this->_redirect('/kontakt/finish/');
            }
        } else {
            $this->view->form = $form;
        }
    }

    public function finishAction() {

    }

}